package com.vhly.aapt;

import com.vhly.aapt.io.OrderInputStream;

import java.io.IOException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-4-26
 * Time: 16:06:44
 */
public class ResXMLTree_attrExt extends ResXMLTree_node {
    public int nsStringPoolIndex;
    public int nameStringPoolIndex;
    public short attributeStart;
    public short attributeSize;
    public short attributeCount;

    public short idIndex;
    public short classIndex;
    public short styleIndex;

    public String ns;

    public Vector<ResXMLTree_attribute> attributes;

    public ResXMLTree_attrExt() {
        attributes = new Vector<ResXMLTree_attribute>();
    }

    @Override
    public String toXML() {
        String ret = "";
        StringBuffer sb = new StringBuffer();
        sb.append('<');
        if(nsStringPoolIndex > -1){
            String nss = pool.entries.get(nsStringPoolIndex);
            int index = nss.lastIndexOf("/");
            if(index != -1){
                nss = nss.substring(index+1);
            }
            sb.append(nss).append(':');
        }
        if(nameStringPoolIndex > -1){
            sb.append(pool.entries.get(nameStringPoolIndex));
        }
        sb.append(' ');
        if(ns != null){
            sb.append("xmlns:").append(ns).append(' ');
        }
        if(attributeCount > 0){
            ResXMLTree_attribute attr;
            for(int i = 0;i<attributeCount;i++){
                attr = attributes.get(i);
                sb.append(attr.toXML()).append(' ');
            }
        }
        sb.append('>');
        ret = sb.toString();
        return ret;
    }

    @Override
    public void read(OrderInputStream din) throws IOException {
        super.read(din);
        nsStringPoolIndex = din.readInt();
        nameStringPoolIndex = din.readInt();
        attributeStart = din.readShort();
        attributeSize = din.readShort();
        attributeCount = din.readShort();
        idIndex = din.readShort();
        classIndex = din.readShort();
        styleIndex = din.readShort();
    }

    @Override
    public short getHeaderSize() {
        return 0;
    }

    @Override
    public byte[] getData() {
        return new byte[0];
    }
}